unit mainform;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, cef3lcl, SpkToolbar, spkt_Tab, spkt_Pane,
  spkt_BaseItem, spkt_Buttons, Forms, Controls, Graphics, Dialogs, ComCtrls,
  Menus, StdCtrls, ExtCtrls, cef3types, cef3intf, cef3gui, cef3context, intf,
  nkTitleBar, nkResizer, rxctrls, JButton, TplSpeedButtonUnit, TplButtonExUnit,
  cyButton, cyAdvSpeedButton, BGRASpeedButton, BCImageButton, BGRANeoButton,
  BGRAButton, BCButton, BCMaterialDesignButton, BGRACustomDrawn, IniFiles,
  LCLIntf, LCLType;

type

  { TfrmMain }

  TfrmMain = class(TForm)
    html: TChromium;
    ImageList1: TImageList;
    Label1: TLabel;
    Label2: TLabel;
    MenuItem1: TMenuItem;
    nkResizer1: TnkResizer;
    nkTitleBar1: TnkTitleBar;
    SpkLargeButton20: TSpkLargeButton;
    SpkLargeButton21: TSpkLargeButton;
    SpkLargeButton22: TSpkLargeButton;
    SpkPane7: TSpkPane;
    SpkPane8: TSpkPane;
    SpkPane9: TSpkPane;
    SpkTab4: TSpkTab;
    SpkTab5: TSpkTab;
    StatusBar1: TPanel;
    RxSpeedButton1: TRxSpeedButton;
    RxSpeedButton2: TRxSpeedButton;
    RxSpeedButton3: TRxSpeedButton;
    RxSpeedButton4: TRxSpeedButton;
    RxSpeedButton5: TRxSpeedButton;
    SpkLargeButton16: TSpkLargeButton;
    SpkLargeButton17: TSpkLargeButton;
    SpkLargeButton18: TSpkLargeButton;
    SpkLargeButton19: TSpkLargeButton;
    SpkTab3: TSpkTab;
    PopupMenu1: TPopupMenu;
    SpkLargeButton1: TSpkLargeButton;
    SpkLargeButton10: TSpkLargeButton;
    SpkLargeButton11: TSpkLargeButton;
    SpkLargeButton12: TSpkLargeButton;
    SpkLargeButton13: TSpkLargeButton;
    SpkLargeButton14: TSpkLargeButton;
    SpkLargeButton15: TSpkLargeButton;
    SpkLargeButton2: TSpkLargeButton;
    SpkLargeButton3: TSpkLargeButton;
    SpkLargeButton4: TSpkLargeButton;
    SpkLargeButton5: TSpkLargeButton;
    SpkLargeButton6: TSpkLargeButton;
    SpkLargeButton7: TSpkLargeButton;
    SpkLargeButton8: TSpkLargeButton;
    SpkLargeButton9: TSpkLargeButton;
    SpkPane1: TSpkPane;
    SpkPane2: TSpkPane;
    SpkPane3: TSpkPane;
    SpkPane4: TSpkPane;
    SpkPane5: TSpkPane;
    SpkPane6: TSpkPane;
    SpkTab1: TSpkTab;
    SpkTab2: TSpkTab;
    SpkToolbar1: TSpkToolbar;
    procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
    procedure FormShow(Sender: TObject);
    procedure htmlAddressChange(Sender: TObject; const Browser: ICefBrowser;
      const Frame: ICefFrame; const url: ustring);
    procedure htmlBeforeBrowse(Sender: TObject; const browser: ICefBrowser;
      const frame: ICefFrame; const request: ICefRequest; isRedirect: Boolean;
      out Result: Boolean);
    procedure htmlBeforeContextMenu(Sender: TObject;
      const Browser: ICefBrowser; const Frame: ICefFrame;
      const params: ICefContextMenuParams; const model: ICefMenuModel);
    procedure htmlBeforeDownload(Sender: TObject; const Browser: ICefBrowser;
      const downloadItem: ICefDownloadItem; const suggestedName: ustring;
      const callback: ICefBeforeDownloadCallback);
    procedure htmlBeforePopup(Sender: TObject; const browser: ICefBrowser;
      const frame: ICefFrame; const targetUrl, targetFrameName: ustring;
      targetDisposition: TCefWindowOpenDisposition; userGesture: Boolean;
      var popupFeatures: TCefPopupFeatures; var windowInfo: TCefWindowInfo;
      var client: ICefClient; var settings: TCefBrowserSettings;
      var noJavascriptAccess: Boolean; out Result: Boolean);
    procedure htmlContextMenuCommand(Sender: TObject;
      const Browser: ICefBrowser; const Frame: ICefFrame;
      const params: ICefContextMenuParams; commandId: Integer;
      eventFlags: TCefEventFlags; out Result: Boolean);
    procedure htmlDownloadUpdated(Sender: TObject; const Browser: ICefBrowser;
      const downloadItem: ICefDownloadItem;
      const callback: ICefDownloadItemCallback);
    procedure htmlLoadEnd(Sender: TObject; const Browser: ICefBrowser;
      const Frame: ICefFrame; httpStatusCode: Integer);
    procedure htmlLoadStart(Sender: TObject; const Browser: ICefBrowser;
      const Frame: ICefFrame; transitionType: TCefTransitionType);
    procedure MenuItem1Click(Sender: TObject);
    procedure RxSpeedButton1Click(Sender: TObject);
    procedure RxSpeedButton2Click(Sender: TObject);
    procedure RxSpeedButton3Click(Sender: TObject);
    procedure RxSpeedButton4Click(Sender: TObject);
    procedure SpkLargeButton11Click(Sender: TObject);
    procedure SpkLargeButton12Click(Sender: TObject);
    procedure SpkLargeButton13Click(Sender: TObject);
    procedure SpkLargeButton14Click(Sender: TObject);
    procedure SpkLargeButton15Click(Sender: TObject);
    procedure SpkLargeButton16Click(Sender: TObject);
    procedure SpkLargeButton17Click(Sender: TObject);
    procedure SpkLargeButton18Click(Sender: TObject);
    procedure SpkLargeButton19Click(Sender: TObject);
    procedure SpkLargeButton1Click(Sender: TObject);
    procedure SpkLargeButton20Click(Sender: TObject);
    procedure SpkLargeButton21Click(Sender: TObject);
    procedure SpkLargeButton22Click(Sender: TObject);
    procedure SpkLargeButton2Click(Sender: TObject);
    procedure SpkLargeButton3Click(Sender: TObject);
    procedure SpkLargeButton4Click(Sender: TObject);
    procedure SpkLargeButton5Click(Sender: TObject);
    procedure SpkLargeButton6Click(Sender: TObject);
    procedure SpkLargeButton7Click(Sender: TObject);
    procedure SpkLargeButton8Click(Sender: TObject);
    procedure SpkLargeButton9Click(Sender: TObject);
  private

  public
    mBtn:TSpkLargeButton;//Boss按钮

    procedure LoadPage(AValue:string);//File协议载入页面，参数是文件名
    procedure LoadUrl(AKey:string;AWS:string='');//从urls浏览页面，参数是关键字
    procedure ParseCMD(AURL:string;var ACMD, AParams:string);//解析页面中的CMD协议字符串，返回命令字和参数字符串（中间用？间隔）
    procedure ExecuteCMD(ACMD, AParams:string);//执行CMD命令
    procedure CustomPageClick(Sender:TObject);//自定义链接点击事件
    procedure SetButtonsEnabled(AValue:boolean);//设置按钮的使能状态
    procedure LoadHisWatchers;//加载定点医药机构实时监控注入表
    procedure WatcherButtonClick(Sender:Tobject);//定点医药机构按钮点击事件
    procedure onBossQueryCilck(Sender:TObject);//领导查询按钮点击事件
    procedure AddBossQueryButton;//增加领导查询按钮
  end;

var
  frmMain: TfrmMain;

implementation

uses
  datamodule, daoru_1form, check_5form, check_1form,
  check2_form, loginform, resetform, syncform, pagesform,
  scriptsform, editframesform;

{$R *.frm}

{ TfrmMain }

procedure TfrmMain.FormShow(Sender: TObject);
var
  mindex:integer;
  murl:TnkUrlItem;
  mIni:TIniFile;
  mSection:string;
  i,j,k:integer;
begin
  //连接公共库
  try
    dm.conn.LibraryLocation:=ExtractFilepath(Application.ExeName)+'libmysql50.dll';
    //读INI设置
    mIni:=TIniFile.Create(ExtractFilepath(Application.ExeName)+'system.ini');
    mSection:='public';
    dm.conn.HostName:=mIni.ReadString(mSection,'server','localhost');
    dm.conn.User:=mIni.ReadString(mSection,'user','root');
    dm.conn.Password:=mIni.ReadString(mSection,'passwd','vertrigo');
    dm.conn.Database:=mIni.ReadString(mSection,'database','neikong');
    dm.conn.Port:=mIni.ReadInteger(mSection,'port',3306);
    mIni.Free;
    dm.conn.Connect;
  except
    MessageDlg('无法连接服务器，请检查配置文件或联系管理员。',mtError,[mbOK],0);
    Self.Hide;
    Application.ShowMainForm:=False;
    Application.Terminate;
  end;
  //放大窗口
  //LCLIntf.ShowWindow(Self.Handle, SW_SHOWFULLSCREEN);
  Self.Top:=Screen.WorkAreaTop;
  Self.Left:=Screen.WorkAreaLeft;
  Self.Width:=Screen.WorkAreaWidth;
  Self.Height:=Screen.WorkAreaHeight;
  //全屏
  RxSpeedButton4Click(nil);
  //载入URLS
  dm.LoadUrls;
  //浏览器导航至home
  mindex:=dm.Urls.IndexOf('home');
  if mindex<>-1 then
  begin
    murl:=dm.Urls.Item(mindex);
    html.DefaultUrl:=murl.proot+murl.purl;
    html.Load(murl.proot+murl.purl);
  end;
  //载入自定义脚本
  Dm.FillScripts;
  //生成自定义按钮
  Dm.FillPages;
  if Length(Dm.Pages)>0 then
  begin
    SpkPane7.Items.Clear;
    for i:=0 to Length(Dm.Pages)-1 do
    begin
      with SpkPane7.Items.AddLargeButton do
      begin
        Caption:=Dm.Pages[i].caption;
        AllowAllUp:=True;
        ButtonKind:=bkToggle;
        LargeImageIndex:=1;
        GroupIndex:=1;
        Tag:=i;
        onclick:=@CustomPageClick;
      end;
    end;
    SpkTab3.Visible:=True;
    SpkPane7.Visible:=True;
  end;
  // 加载定点医药机构注入数据
  LoadHisWatchers;
  if Length(Dm.HisWatcher)>0 then
  begin
    //SpkPane8.Items.Clear;
    for i:=0 to Length(Dm.HisWatcher)-1 do
    begin
      case dm.HisWatcher[i].page of
        0:
          begin
            with SpkPane8.Items.AddLargeButton do
            begin
              Caption:=Dm.HisWatcher[i].caption;
              Tag:=Dm.HisWatcher[i].tag;
              AllowAllUp:=True;
              ButtonKind:=bkToggle;
              LargeImageIndex:=1;
              GroupIndex:=1;
              onclick:=@WatcherButtonClick;
            end;
          end;
        1:
          begin
            with SpkPane9.Items.AddLargeButton do
            begin
              Caption:=Dm.HisWatcher[i].caption;
              Tag:=Dm.HisWatcher[i].tag;
              AllowAllUp:=True;
              ButtonKind:=bkToggle;
              LargeImageIndex:=1;
              GroupIndex:=1;
              onclick:=@WatcherButtonClick;
            end;
          end;
      end;

    end;
    SpkTab4.Visible:=True;
    SpkPane8.Visible:=True;
    SpkTab5.Visible:=True;
    SpkPane9.Visible:=True;
  end;
  //加载本地页面
  //html.Load('file://'+ExtractFilepath(Application.ExeName)+'/data/welcome/index.html')
  //禁用除16外的所有按钮
  AddBossQueryButton;
  SetButtonsEnabled(False);
  SpkLargeButton19.Enabled:=True;
end;

procedure TfrmMain.htmlAddressChange(Sender: TObject;
  const Browser: ICefBrowser; const Frame: ICefFrame; const url: ustring);
begin

end;

procedure TfrmMain.FormCloseQuery(Sender: TObject; var CanClose: boolean);
begin
  Application.MainForm.Hide;
  CanClose:=True;
end;

procedure TfrmMain.htmlBeforeBrowse(Sender: TObject;
  const browser: ICefBrowser; const frame: ICefFrame;
  const request: ICefRequest; isRedirect: Boolean; out Result: Boolean);
var
  mCMD, mParams:string;
begin
  //检查点击的链接类型，如果是CMD协议则解析命令URL，并执行相应动作
  if Pos('cmd://',lowercase(trim(request.Url))) > 0 then
  begin
    ParseCMD(request.Url, mCmd, mParams);
    ExecuteCMD(mCmd, mParams);
    Result:=True;
  end
  else
    Result:=False;
end;

procedure TfrmMain.htmlBeforeContextMenu(Sender: TObject;
  const Browser: ICefBrowser; const Frame: ICefFrame;
  const params: ICefContextMenuParams; const model: ICefMenuModel);
begin
  model.Clear;
end;

procedure TfrmMain.htmlBeforeDownload(Sender: TObject;
  const Browser: ICefBrowser; const downloadItem: ICefDownloadItem;
  const suggestedName: ustring; const callback: ICefBeforeDownloadCallback);
begin
  callback.Cont(suggestedName, True);
end;

procedure TfrmMain.htmlBeforePopup(Sender: TObject; const browser: ICefBrowser;
  const frame: ICefFrame; const targetUrl, targetFrameName: ustring;
  targetDisposition: TCefWindowOpenDisposition; userGesture: Boolean;
  var popupFeatures: TCefPopupFeatures; var windowInfo: TCefWindowInfo;
  var client: ICefClient; var settings: TCefBrowserSettings;
  var noJavascriptAccess: Boolean; out Result: Boolean);
begin
  //html.Load(targeturl);
  result:=false;
end;

procedure TfrmMain.htmlContextMenuCommand(Sender: TObject;
  const Browser: ICefBrowser; const Frame: ICefFrame;
  const params: ICefContextMenuParams; commandId: Integer;
  eventFlags: TCefEventFlags; out Result: Boolean);
begin

end;

procedure TfrmMain.htmlDownloadUpdated(Sender: TObject;
  const Browser: ICefBrowser; const downloadItem: ICefDownloadItem;
  const callback: ICefDownloadItemCallback);
begin
  if downloadItem.GetTotalBytes<>downloadItem.GetReceivedBytes then
  begin
    Label1.Caption:='正在下载……';
  end;
  if downloadItem.IsComplete then
    Label1.Caption:='下载完毕。';
  if downloadItem.IsCanceled then
    Label1.Caption:='下载已取消。';

end;

procedure TfrmMain.htmlLoadEnd(Sender: TObject; const Browser: ICefBrowser;
  const Frame: ICefFrame; httpStatusCode: Integer);
var
  js:string;
  muser,mpwd:string;
begin
  Label1.Caption:='';//Frame.url;
  //新加的地址
  if Frame.Url='http://118.190.207.144/hosDrug/login' then
  begin
    js:='document.getElementsByName("username")[0].value="cysrmyy";document.getElementsByName("password")[0].value="123456";document.getElementById("btnSubmit").click();';
    Frame.ExecuteJavaScript(js,Frame.url,0);
  end;
  //测试填表
  if Frame.Url='http://47.104.80.20:8090/zyybpt-web/msunLogin' then
  begin
    js:='document.getElementsByName("uid")[0].value="002";document.getElementsByName("pwd")[0].value="1";document.getElementById("login").click();';
    Frame.ExecuteJavaScript(js,Frame.url,0);
  end;
  //测试转向
  if Frame.Url='http://47.104.80.20:8090/zyybpt-web/jsp/qcms/welcomePage.jsp' then
  begin
    js:='var navs=document.getElementsByClassName("nav-text");'
        +'var x=navs.length;for(let i=0;i<x;i++){if((navs[i].innerHTML=="进销存监管大屏")){navs[i].click();}}';
    Frame.Browser.MainFrame.ExecuteJavaScript(js,Frame.Browser.MainFrame.url,0);
  end;
  //实时监控注入，根据状态机决定注入密码
//  if Frame.Url='http://10.118.1.186:8015/mhis/login.html?2' then
  if Frame.Url='http://10.79.207.108:8002/mhis/login.html?2' then
  begin
    mUser:=dm.HisWatcher[dm.HisWatcherTag].user;
    mPwd:=dm.HisWatcher[dm.HisWatcherTag].pwd;
    js:='var t=document.getElementsByName("j_username")[0];evt=document.createEvent("HTMLEvents");evt.initEvent("input",true,true);t.value="'+mUser+'";t.dispatchEvent(evt);'
        +'var t2=document.getElementsByName("j_password")[0];evt2=document.createEvent("HTMLEvents");evt2.initEvent("input",true,true);t2.value="'+mPwd+'";t2.dispatchEvent(evt2);'
        +'document.getElementById("submit").click();';
    Frame.ExecuteJavaScript(js,Frame.url,0);
  end;
  //医保局监控
  if Frame.Url='http://10.79.207.108:8002/mhis/login.html?1' then
  begin
    mUser:=Dm.JuWatchUser;//'wfcy002';//dm.HisWatcher[dm.HisWatcherTag].user;
    mPwd:=Dm.JuWatchPwd;//'123456';//dm.HisWatcher[dm.HisWatcherTag].pwd;
    js:='var t=document.getElementsByName("j_username")[0];evt=document.createEvent("HTMLEvents");evt.initEvent("input",true,true);t.value="'+mUser+'";t.dispatchEvent(evt);'
        +'var t2=document.getElementsByName("j_password")[0];evt2=document.createEvent("HTMLEvents");evt2.initEvent("input",true,true);t2.value="'+mPwd+'";t2.dispatchEvent(evt2);'
        +'document.getElementById("submit").click();';
    Frame.ExecuteJavaScript(js,Frame.url,0);
  end;
  //事后模块
  if Frame.Url='http://10.79.207.114/sso/#/login' then
  begin
    mUser:='cyswd001';
    mPwd:='1234567b';
    js:='var t=document.getElementsByName("accountNo")[0];evt=document.createEvent("HTMLEvents");evt.initEvent("input",true,true);t.value="'+mUser+'";t.dispatchEvent(evt);'
        +'var t2=document.getElementsByName("password")[0];evt2=document.createEvent("HTMLEvents");evt2.initEvent("input",true,true);t2.value="'+mPwd+'";t2.dispatchEvent(evt2);'
        +'document.getElementsByTagName("button")[0].click();';
    Frame.ExecuteJavaScript(js,Frame.Url,0);
  end;
end;

procedure TfrmMain.htmlLoadStart(Sender: TObject; const Browser: ICefBrowser;
  const Frame: ICefFrame; transitionType: TCefTransitionType);
begin
  Label1.Caption:='正在载入数据……';
end;

procedure TfrmMain.MenuItem1Click(Sender: TObject);
begin
  //查看职工医疗个人账户划拨风险分析结果
  LoadUrl('analreport5',Dm.Platform);
end;

procedure TfrmMain.RxSpeedButton1Click(Sender: TObject);
begin
  Close;
end;

procedure TfrmMain.RxSpeedButton2Click(Sender: TObject);
begin
  Self.Top:=Self.Monitor.WorkareaRect.Top;
  Self.Left:=Self.Monitor.WorkareaRect.Left;
  Self.Width:=Self.Monitor.WorkareaRect.Width;
  Self.Height:=Self.Monitor.WorkareaRect.Height;
{  Self.Top:=Screen.WorkAreaTop;
  Self.Left:=Screen.WorkAreaLeft;
  Self.Width:=Screen.WorkAreaWidth;
  Self.Height:=Screen.WorkAreaHeight;}
end;

procedure TfrmMain.RxSpeedButton3Click(Sender: TObject);
begin
  Application.Minimize;
end;

procedure TfrmMain.RxSpeedButton4Click(Sender: TObject);
begin
  SpkToolBar1.Visible:=not SpkToolBar1.Visible;
  StatusBar1.Visible:=SpkToolBar1.Visible;
end;


procedure TfrmMain.SpkLargeButton11Click(Sender: TObject);
begin
  //弹出自定义页面管理窗口
  frmPages:=TfrmPages.Create(Application);
  frmPages.ShowModal;
  frmPages.free;
  frmPages:=nil;
end;

procedure TfrmMain.SpkLargeButton12Click(Sender: TObject);
begin
  LoadUrl('importguide',Dm.Platform);
end;

procedure TfrmMain.SpkLargeButton13Click(Sender: TObject);
begin
  LoadUrl('bigdata',Dm.Platform);
end;

procedure TfrmMain.SpkLargeButton14Click(Sender: TObject);
begin
  //系统复位窗口
  frmReset:=TfrmReset.Create(Application);
  frmReset.ShowModal;
  frmReset.Free;
  frmReset:=nil;
end;

procedure TfrmMain.SpkLargeButton15Click(Sender: TObject);
begin
  //同步基础数据库，从公共数据库复制数据到工作库
  frmSync:=TfrmSync.Create(Application);
  frmSync.showModal;
  frmSync.free;
  frmSync:=nil;
end;

procedure TfrmMain.SpkLargeButton16Click(Sender: TObject);
begin
  //弹出自定义页面管理窗口
  frmPages:=TfrmPages.Create(Application);
  frmPages.ShowModal;
  frmPages.free;
  frmPages:=nil;
end;

procedure TfrmMain.SpkLargeButton17Click(Sender: TObject);
begin
  //弹出自动化例程管理
    frmScripts:=TfrmScripts.Create(application);
    frmScripts.ShowModal;
    frmScripts.Free;
    frmScripts:=nil;
end;

procedure TfrmMain.SpkLargeButton18Click(Sender: TObject);
var
  mIndex:integer;
  mUrl:TnkUrlItem;
begin
  //浏览器导航至home
  mindex:=dm.Urls.IndexOf('bigdata');
  if mindex<>-1 then
  begin
    murl:=dm.Urls.Item(mindex);
    html.Load(murl.proot+murl.purl);
  end;
end;

procedure TfrmMain.SpkLargeButton19Click(Sender: TObject);
begin
  //全屏
  if SpkToolbar1.Visible then
    RxSpeedButton4Click(nil);
  //禁用按钮
  SetButtonsEnabled(False);
  SpkLargeButton19.Enabled:=True;
  mBtn.Visible:=False;
  LoadUrl('home',dm.Platform);
  dm.Platform:='';
end;

procedure TfrmMain.SpkLargeButton1Click(Sender: TObject);
begin
  //LoadPage(FENXI_HTML);
  LoadUrl('anal',Dm.Platform);
end;

procedure TfrmMain.SpkLargeButton20Click(Sender: TObject);
begin
  html.Load('http://10.79.207.108:8002/mhis/login.html?1');
end;

procedure TfrmMain.SpkLargeButton21Click(Sender: TObject);
begin
  //显示自定义大屏窗口
  frmEditFrames:=TfrmEditFrames.Create(Application);
  frmEditFrames.ShowModal;
  frmEditFrames.Free;
  frmEditFrames:=nil;
end;

procedure TfrmMain.SpkLargeButton22Click(Sender: TObject);
begin
  html.Load('http://10.79.207.114/sso/#/login');
end;

procedure TfrmMain.SpkLargeButton2Click(Sender: TObject);
begin
  html.Browser.Reload;
end;

procedure TfrmMain.SpkLargeButton3Click(Sender: TObject);
begin
  //报告分类浏览
  LoadUrl('reportcatalog',Dm.Platform);
end;

procedure TfrmMain.SpkLargeButton4Click(Sender: TObject);
begin
  //数据分类浏览
  LoadUrl('datacatalog',Dm.Platform);
end;

procedure TfrmMain.SpkLargeButton5Click(Sender: TObject);
begin
  //搜索查询
  LoadUrl('search',Dm.Platform);
end;

procedure TfrmMain.SpkLargeButton6Click(Sender: TObject);
begin
  //浏览至import
  LoadUrl('import',Dm.Platform);
//  LoadPage(DAORU_HTML);
end;

procedure TfrmMain.SpkLargeButton7Click(Sender: TObject);
var
  mindex:integer;
  murl:TnkUrlItem;
begin
  //浏览到home
  LoadUrl('dashboard', Dm.Platform);
end;

procedure TfrmMain.SpkLargeButton8Click(Sender: TObject);
begin
  if html.Browser.CanGoBack then
    html.Browser.GoBack;
end;

procedure TfrmMain.SpkLargeButton9Click(Sender: TObject);
begin
  if html.Browser.CanGoForward then
    html.Browser.GoForward;
end;

procedure TfrmMain.LoadPage(AValue: string);
var
  mFile:string;
begin
  mFile:=ExtractFilepath(Application.ExeName)+AValue;
  html.Load('file://'+mFile);
end;

procedure TfrmMain.LoadUrl(AKey: string;AWS:string='');
var
  mindex:integer;
  murl:TnkUrlItem;
  m:string;
begin
  mindex:=dm.Urls.IndexOf(AKey);
  if mindex<>-1 then
  begin
    murl:=dm.Urls.Item(mindex);
    m:=murl.proot+murl.purl;
    if trim(AWS)<>'' then
    begin
      //加入工作库参数
      if pos('?',m)>0 then
      begin
        m:=m+'&ws='+Trim(AWS);
      end
      else
      begin
        m:=m+'?ws='+Trim(AWS);
      end;
    end;
    html.Load(m);
  end;
end;

procedure TfrmMain.ParseCMD(AURL: string; var ACMD, AParams: string);
var
  mPos:integer;
  mTmp:string;
begin
  //解析页面中的CMD协议字符串，返回命令字和参数字符串（中间用？间隔）
  if Pos('cmd://',lowercase(Trim(AURL))) > 0 then
  begin
    mTmp:=Copy(LowerCase(AURL),7,Length(AURL));
    mPos:=Pos('?', mTmp);
    if mPos > 0 then
    begin
      ACMD:=Copy(mTmp, 0, mPos-1);
      AParams:=Copy(mTmp,mPos+1,length(mTmp));
    end
    else
    begin
      ACMd:=mTmp;
      AParams:='';
    end;
  end;
end;

procedure TfrmMain.ExecuteCMD(ACMD, AParams: string);
begin
  //执行CMD命令
  case ACMD of
    DOSYNC: //同步基础库
    begin
      SpkLargeButton15Click(nil);
    end;
    DOLOGIN:  //登录
    begin
      //登录
      frmLogin:=TfrmLogin.create(Application);
      if frmLogin.ShowModal=mrOK then
      begin
        //根据登录名和密码返回工作库信息
        dm.connWork.LibraryLocation:=ExtractFilepath(Application.ExeName)+'libmysql50.dll';
        dm.connWork.HostName:=frmLogin.dbhost;
        dm.connWork.Database:=frmLogin.dbname;
        dm.connWork.User:=frmLogin.dbuser;
        dm.connWork.Password:=frmLogin.dbpwd;
        dm.connWork.Port:=frmLogin.dbport;
      end
      else
      begin
        //LoadUrl('home',dm.Platform);
        frmLogin.free;
        frmLogin:=nil;
        Exit;
      end;
      Dm.Platform:=frmLogin.ComboBox1.Text;
      frmLogin.free;
      frmLogin:=nil;
      //如果是公共库登录，则在SpkPane5中增加新按钮，显示领导查询
      if Dm.Platform='公共库' then
        //AddBossQueryButton;改为Boss按钮可见
        mBtn.Visible:=True
      else
        mBtn.Visible:=False;

      //连接工作库
      try
        dm.connWork.Connect;
        //状态栏显示当前数据库
        Label2.Caption:='当前工作库：'+Dm.Platform;
        //LoadUrl('dashboard',dm.Platform);
        html.Load('http://'+dm.conn.HostName+'/neikong/index.php?c=bigdata&ws='+Dm.Platform);
        //启用所有按钮
        SetButtonsEnabled(True);
        //显示工具栏
        if not SpkToolbar1.Visible then
          RxSpeedButton4Click(nil);
      except
        MessageDlg('无法连接工作库，请检查配置文件或联系管理员。',mtError,[mbOK],0);
      end;
    end;
    DOTOGGLE: //切换全屏
    begin
      frmMain.BeginFormUpdate;
      StatusBar1.Visible:=not SpkToolbar1.Visible;
      SpkToolbar1.Visible:=not SpkToolbar1.Visible;
      frmMain.EndFormUpdate;
    end;
    DAORU_1:  //导入职工医疗个人账户划拨表
      begin
        frmDaoru_1:=TfrmDaoru_1.Create(Application);
        frmDaoru_1.Kind:=tikHuabo;
        frmDaoru_1.ShowModal;
        frmDaoru_1.free;
        frmDaoru_1:=nil;
        html.Browser.Reload;
      end;
    DAORU_2:  //导入死亡人口表
      begin
        frmDaoru_1:=TfrmDaoru_1.Create(Application);
        frmDaoru_1.Kind:=tikSiwang;
        frmDaoru_1.ShowModal;
        frmDaoru_1.free;
        frmDaoru_1:=nil;
        html.Browser.Reload;
      end;
    DAORU_3:  //导入单位信息表
      begin
        frmDaoru_1:=TfrmDaoru_1.Create(Application);
        frmDaoru_1.Kind:=tikdanwei;
        frmDaoru_1.ShowModal;
        frmDaoru_1.free;
        frmDaoru_1:=nil;
        html.Browser.Reload;
      end;
    DAORU_4:  //导入企业注销名单
      begin
        frmDaoru_1:=TfrmDaoru_1.Create(Application);
        frmDaoru_1.Kind:=tikzhuxiao;
        frmDaoru_1.ShowModal;
        frmDaoru_1.free;
        frmDaoru_1:=nil;
        html.Browser.Reload;
      end;
    DAORU_5:  //导入学生居民医疗保险参保登记表
      begin
        frmDaoru_1:=TfrmDaoru_1.Create(Application);
        frmDaoru_1.Kind:=tikXuesheng;
        frmDaoru_1.ShowModal;
        frmDaoru_1.free;
        frmDaoru_1:=nil;
        html.Browser.Reload;
      end;
    DAORU_6:  //导入职工医疗保险转出名单表
      begin
        frmDaoru_1:=TfrmDaoru_1.Create(Application);
        frmDaoru_1.Kind:=tikZhuanchu;
        frmDaoru_1.ShowModal;
        frmDaoru_1.free;
        frmDaoru_1:=nil;
        html.Browser.Reload;
      end;
    DAORU_7:  //职工医疗保险补缴表
      begin
        frmDaoru_1:=TfrmDaoru_1.Create(Application);
        frmDaoru_1.Kind:=tikBujiao;
        frmDaoru_1.ShowModal;
        frmDaoru_1.free;
        frmDaoru_1:=nil;
        html.Browser.Reload;
      end;
    DAORU_8:  //居民医疗保险参保人员名单表
      begin
        frmDaoru_1:=TfrmDaoru_1.Create(Application);
        frmDaoru_1.Kind:=tikJumin;
        frmDaoru_1.ShowModal;
        frmDaoru_1.free;
        frmDaoru_1:=nil;
        html.Browser.Reload;
      end;
    DAORU_9:  //职工医疗参保人员名单
    begin
      frmDaoru_1:=TfrmDaoru_1.Create(Application);
      frmDaoru_1.Kind:=tikZhigong;
      frmDaoru_1.ShowModal;
      frmDaoru_1.free;
      frmDaoru_1:=nil;
      html.Browser.Reload;
    end;
    DAORU_20: //高血压、糖尿病办理登记表
      begin
        frmDaoru_1:=TfrmDaoru_1.Create(Application);
        frmDaoru_1.Kind:=tikGaoxueya;
        frmDaoru_1.ShowModal;
        frmDaoru_1.free;
        frmDaoru_1:=nil;
        html.Browser.Reload;
      end;
    DAORU_21: //居民慢性病办理名单表
      begin
        frmDaoru_1:=TfrmDaoru_1.Create(Application);
        frmDaoru_1.Kind:=tikManxingbing;
        frmDaoru_1.ShowModal;
        frmDaoru_1.free;
        frmDaoru_1:=nil;
        html.Browser.Reload;
      end;
    DAORU_22: //居民慢性病名称和编码表
      begin
        frmDaoru_1:=TfrmDaoru_1.Create(Application);
        frmDaoru_1.Kind:=tikManbingBianma;
        frmDaoru_1.ShowModal;
        frmDaoru_1.free;
        frmDaoru_1:=nil;
        html.Browser.Reload;
      end;
    DAORU_23: //定点医院名称编码级别对应表
      begin
        frmDaoru_1:=TfrmDaoru_1.Create(Application);
        frmDaoru_1.Kind:=tikYiyuanBianma;
        frmDaoru_1.ShowModal;
        frmDaoru_1.free;
        frmDaoru_1:=nil;
        html.Browser.Reload;
      end;
    DAORU_24: //职工慢性病名称和编码表
      begin
        frmDaoru_1:=TfrmDaoru_1.Create(Application);
        frmDaoru_1.Kind:=tikZhigongmanbingBianma;
        frmDaoru_1.ShowModal;
        frmDaoru_1.free;
        frmDaoru_1:=nil;
        html.Browser.Reload;
      end;
    DAORU_25: //职工慢性病办理名单表
    begin
      frmDaoru_1:=TfrmDaoru_1.Create(Application);
      frmDaoru_1.Kind:=tikZhigongManxingbing;
      frmDaoru_1.ShowModal;
      frmDaoru_1.free;
      frmDaoru_1:=nil;
      html.Browser.Reload;
    end;
    CHECK_5:  //职工医疗账户划拨风险分析
      begin
        frmCheck_5:=TfrmCheck_5.Create(Application);
        frmCheck_5.showmodal;
        frmCheck_5.free;
        frmCheck_5:=nil;
        html.Browser.Reload;
      end;
    CHECK_1:  //单位职工医疗参保登记风险分析
      begin
        frmCheck_1:=TfrmCheck_1.Create(Application);
        frmCheck_1.Kind:=TikDanwei;
        frmCheck_1.ShowModal;
        frmCheck_1.free;
        frmCheck_1:=nil;
        html.Browser.Reload;
      end;
    CHECK_11:               //单位注销登记风险
      begin
        frmCheck_1:=TfrmCheck_1.Create(Application);
        frmCheck_1.Kind:=TikZhuxiao;
        frmCheck_1.ShowModal;
        frmCheck_1.free;
        frmCheck_1:=nil;
        html.Browser.Reload;
      end;
    CHECK_2:                //职工基本医疗保险参保登记风险
      begin
        frmCheck_2:=TfrmCheck_2.Create(Application);
        frmCheck_2.kind:=tikZhigong;
        frmCheck_2.ShowModal;
        frmCheck_2.Free;
        frmCheck_2:=nil;
        html.Browser.Reload;
      end;
    CHECK_21:               //职工医疗保险补缴业务风险
      begin
        frmCheck_2:=TfrmCheck_2.Create(Application);
        frmCheck_2.kind:=tikBujiao;
        frmCheck_2.ShowModal;
        frmCheck_2.Free;
        frmCheck_2:=nil;
        html.Browser.Reload;
      end;
    CHECK_3:                //居民医疗保险参保登记办理风险
      begin
        frmCheck_2:=TfrmCheck_2.Create(Application);
        frmCheck_2.kind:=tikJumin;
        frmCheck_2.ShowModal;
        frmCheck_2.Free;
        frmCheck_2:=nil;
        html.Browser.Reload;
      end;
    CHECK_31:               //居民参保人员重复信息合并风险
      begin
        frmCheck_2:=TfrmCheck_2.Create(Application);
        frmCheck_2.kind:=tikHebing;
        frmCheck_2.ShowModal;
        frmCheck_2.Free;
        frmCheck_2:=nil;
        html.Browser.Reload;
      end;
    CHECK_4:                //职工医疗保险转出办理风险
      begin
        frmCheck_2:=TfrmCheck_2.Create(Application);
        frmCheck_2.kind:=tikZhuanchu;
        frmCheck_2.ShowModal;
        frmCheck_2.Free;
        frmCheck_2:=nil;
        html.Browser.Reload;
      end;
    CHECK_6:                //学生居民医疗保险参保登记办理风险
      begin
        frmCheck_2:=TfrmCheck_2.Create(Application);
        frmCheck_2.kind:=tikXuesheng;
        frmCheck_2.ShowModal;
        frmCheck_2.Free;
        frmCheck_2:=nil;
        html.Browser.Reload;
      end;
    CHECK_7:                //居民慢性病办理风险
      begin
        frmCheck_2:=TfrmCheck_2.Create(Application);
        frmCheck_2.kind:=tikManxingbing;
        frmCheck_2.ShowModal;
        frmCheck_2.Free;
        frmCheck_2:=nil;
        html.Browser.Reload;
      end;
    CHECK_8:                //职工慢性病办理风险
      begin
        frmCheck_2:=TfrmCheck_2.Create(Application);
        frmCheck_2.kind:=tikZhigongManxingbing;
        frmCheck_2.ShowModal;
        frmCheck_2.Free;
        frmCheck_2:=nil;
        html.Browser.Reload;
      end;
    CHECK_9:                //城乡居民高血压、糖尿病办理风险
      begin
        frmCheck_2:=TfrmCheck_2.Create(Application);
        frmCheck_2.kind:=tikGaoxueya;
        frmCheck_2.ShowModal;
        frmCheck_2.Free;
        frmCheck_2:=nil;
        html.Browser.Reload;
      end;
    CHECK_71:               //死亡居民享受慢病待遇风险
      begin
        frmCheck_2:=TfrmCheck_2.Create(Application);
        frmCheck_2.kind:=tikSiwangjuminmanbing;
        frmCheck_2.ShowModal;
        frmCheck_2.Free;
        frmCheck_2:=nil;
        html.Browser.Reload;
      end;
    CHECK_81:               //死亡职工享受慢病待遇风险
      begin
        frmCheck_2:=TfrmCheck_2.Create(Application);
        frmCheck_2.kind:=tikSiwangzhigongmanbing;
        frmCheck_2.ShowModal;
        frmCheck_2.Free;
        frmCheck_2:=nil;
        html.Browser.Reload;
      end;
    CHECK_91:               //死亡居民享受两病待遇风险
      begin
        frmCheck_2:=TfrmCheck_2.Create(Application);
        frmCheck_2.kind:=tikSiwangliangbing;
        frmCheck_2.ShowModal;
        frmCheck_2.Free;
        frmCheck_2:=nil;
        html.Browser.Reload;
      end;
  end;
end;

procedure TfrmMain.CustomPageClick(Sender: TObject);
var
  mUrl:string;
begin
  mUrl:=Dm.Pages[(Sender as TSpkLargeButton).Tag].url;
  html.Load(mUrl);
end;

procedure TfrmMain.SetButtonsEnabled(AValue: boolean);
var
  i,j,k:integer;
begin
  for i:=0 to SpkToolbar1.Tabs.Count-1 do
  begin
    for j:=0 to SpkToolbar1.Tabs.Items[i].Panes.Count-1 do
    begin
      for k:=0 to SpkToolbar1.Tabs.Items[i].Panes.Items[j].Items.Count-1 do
      begin
        SpkToolbar1.Tabs.Items[i].Panes.Items[j].Items.Items[k].Enabled:=AValue;
      end;
    end;
  end;
end;

procedure TfrmMain.LoadHisWatchers;
var
  mIni:TIniFile;
  mSections:TStrings;
  i, mCount:integer;
begin
  mIni:=TInifile.Create(ExtractFilepath(Application.ExeName)+'watcher.ini');
  mSections:=TStringList.Create;
  mIni.ReadSections(mSections);
  mCount:=mSections.Count;
  if mCount>0 then
  begin
    SetLength(Dm.HisWatcher,mCount);
    for i:=0 to mCount-1 do
    begin
      Dm.HisWatcher[i].tag:=mIni.ReadInteger(mSections.Strings[i],'tag',0);
      Dm.HisWatcher[i].page:=mIni.ReadInteger(mSections.Strings[i],'page',0);
      Dm.HisWatcher[i].caption:=mIni.ReadString(mSections.Strings[i],'caption','');
      Dm.HisWatcher[i].user:=mIni.ReadString(mSections.Strings[i],'user','');
      Dm.HisWatcher[i].pwd:=mIni.ReadString(mSections.Strings[i],'pwd','');
    end;
  end;
  mSections.Free;
  mIni.Free;
  //加载医保局监控的用户名和密码
  mIni:=TIniFile.Create(ExtractFilepath(Application.ExeName)+'watcher2.ini');
  Dm.JuWatchPwd:=mIni.ReadString('common','pwd','');
  Dm.JuWatchUser:=mIni.ReadString('common','user','');
  mIni.Free;
end;

procedure TfrmMain.WatcherButtonClick(Sender: Tobject);
begin
  //设置状态机
  Dm.HisWatcherTag:=(Sender as TSpkLargeButton).Tag;
  //引导页面转向
//  html.Load('http://10.118.1.186:8015/mhis/login.html');
  html.Load('http://10.79.207.108:8002/mhis/login.html?2');
end;

procedure TfrmMain.onBossQueryCilck(Sender: TObject);
begin
  LoadUrl('bossquery');
end;

procedure TfrmMain.AddBossQueryButton;
begin
  mBtn:=SpkPane5.Items.AddLargeButton;
  mBtn.Caption:='领导查询';
  mBtn.LargeImageIndex:=9;
  mBtn.OnClick:=@onBossQueryCilck;
  mBtn.Visible:=False;
end;

end.

